package test1;

/**
 * AggregateAmount
 */
public class MoneyAmount {
  private double moneyAmount;
  private final String[] CAPITAL_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
  private final String[] NUMBER_UNIT = { "分", "角", "元", "拾", "佰", "仟", "万", "亿" };

  MoneyAmount(double moneyAmount) {
    this.moneyAmount = moneyAmount;
  }

  @Override
  public String toString() {
    String ret = "";
    int num = (int) (this.moneyAmount * 100);
    String moneyAmount = String.valueOf(num);
    int unitNum = num % 10;
    num = num / 10;

    for (int i = 0, j = 0; i < moneyAmount.length(); i++, j++, unitNum = num % 10, num = num / 10) {
      // 处理零分的情况
      if (0 == i && 0 == unitNum) {
        continue;
      }
      // 处理零角的情况
      if (1 == i && 0 == unitNum) {
        continue;
      }
      // 处理中间连续零
      if (0 == unitNum && 0 == (num % 10)) {
        continue;
      }
      // 数字单位
      if (0 != unitNum) {
        ret = this.NUMBER_UNIT[j] + ret;
      }
      // 汉字数字
      ret = this.CAPITAL_NUMBER[unitNum] + ret;
    }

    // 整
    if (this.moneyAmount == (int) this.moneyAmount) {
      ret += "元整";
    }
    // 处理中间连续零
    if (-1 != ret.indexOf("元元")) {
      ret = ret.replaceAll("\u5143\u5143", "元");
    }
    // 处理末尾零，10的省略表示形式
    if (-1 != ret.indexOf("零元")) {
      ret = ret.replaceAll("\u96f6\u5143", "元");
    }
    return ret;
  }

  public static void main(String[] args) {
    MoneyAmount moneyAmount1 = new MoneyAmount((double) 123);
    System.out.println(moneyAmount1.toString()); // 壹佰贰拾叁元整

    MoneyAmount moneyAmount2 = new MoneyAmount((double) 10005);
    System.out.println(moneyAmount2.toString()); // 壹万零伍元整

    MoneyAmount moneyAmount3 = new MoneyAmount((double) 110);
    System.out.println(moneyAmount3.toString()); // 壹佰壹拾元整
  }
}